//JavaScript Framework 2.0 Code
try{
Type.registerNamespace('com.yonyou.ncmm_pac_pda.Trans4QMakeListController');
com.yonyou.ncmm_pac_pda.Trans4QMakeListController = function() {
    com.yonyou.ncmm_pac_pda.Trans4QMakeListController.initializeBase(this);
    this.initialize();
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$initialize(){
    //you can programing by $ctx API
    //get the context data through $ctx.get()
    //set the context data through $ctx.push(json)
    //set the field of the context through $ctx.put(fieldName, fieldValue)
    //get the parameter of the context through $ctx.param(parameterName)
    //Demo Code:
    //    var str = $ctx.getString();      //获取当前Context对应的字符串
    //    alert($ctx.getString())          //alert当前Context对应的字符串
    //    var json = $ctx.getJSONObject(); //获取当前Context，返回值为json
    //    json["x"] = "a";        //为当前json增加字段
    //    json["y"] = [];           //为当前json增加数组
    //    $ctx.push(json);            //设置context，并自动调用数据绑定
    //    
    //    put方法需手动调用databind()
    //    var x = $ctx.get("x");    //获取x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.put("x", "b");     //设置x字段值
    //    $ctx.databind();            //调用数据绑定才能将修改的字段绑定到控件上
    //    var p1 = $param.getString("p1");   //获取参数p2的值，返回一个字符串
    //    var p2 = $param.getJSONObject("p2");   //获取参数p3的值，返回一个JSON对象
    //    var p3 = $param.getJSONArray("p3");   //获取参数p1的值，返回一个数组
    
    //your initialize code below...
    
}
    
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$evaljs(js){
    eval(js)
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$close(sender, args){
    $view.close();
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showMenu(sender, args){
    TransMake4QUtil.openScanDropDownList("btn_menu");
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doChecked(sender, args){
    var isCheckedAll = true;
    $ctx.dataCollect();
    var sourceData = $ctx.getJSONArray("listdata");
    for(var i = 0; i < sourceData.length; i++) {
        if(sourceData[i]["isChecked"] == null || sourceData[i]["isChecked"] == false || sourceData[i]["isChecked"] == "false") {
            isCheckedAll = false;
        }
    }
    $ctx.put("scanlist", sourceData);
    $ctx.put("isAllChecked", isCheckedAll);
    $ctx.dataBind();
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doAllChecked(sender, args){
    $ctx.dataCollect();
    var isChecked = $id(sender).get("checked");
    var sourceData = $ctx.getJSONArray("listdata");
    for(var i = 0; i < sourceData.length; i++) {
        sourceData[i]["isChecked"] = isChecked;
    }
    var json = {};
    json["listdata"] = sourceData;
    json["isAllChecked"] = isChecked;
    $ctx.push(json);
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$loadScanList(sender, args){
    loadScanListData();
}
    //加载货位调整单单自制扫描记录
	function loadScanListData() {
	    NCMMPubUtil.queryVOByConditions(Trans4QMakeVO, null, function(data) {
	        var showData = eval(data);
	        showData = NCMMPubUtil.htmlspecialjsonarray2UI(showData);
	        var jsondata = {
			    listdata: eval(showData)
			};
			console.log($jsonToString(jsondata));
			$ctx.push(jsondata);
			$ctx.dataBind();
	    });
	}
	var popDialogInfo = {
        "viewid": "panel_btn",
        "popgravity": "popbottom"
    }
    function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showDetail(sender, args) {
        $view.closePop(popDialogInfo);
        var row = $id("listview").get("row");
        $view.open({
            "viewid": "com.yonyou.ncmm_pac_pda.Trans4QMakeDetail",
            "isKeep": "true",
            "row": row
        });
    }
    function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doScan(sender, args) {
        $view.closePop(popDialogInfo);
        var row = $id("listview").get("row");
        $view.open({
            "viewid": "com.yonyou.ncmm_pac_pda.Trans4QMake",
            "isKeep": "true",
            "row": row
        });
    }
    function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$itemCancel(sender, args) {
        $view.closePop(popDialogInfo);
    }
    function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$itemDelete(sender, args) {
        $view.closePop(popDialogInfo);
        var params = {
			"title": "提示",
			"message": "确定删除所选记录吗？",
			"okbuttontitle": "确定",
			"cancelbuttontitle": "取消",
			"style": "ok-cancel",
			"okaction": "doDelete()",
			"cancelaction": "doCancel()"
		};
		$view.openDialog(params);
    }
    function doDelete() {
		var row = $id("listview").get("row");
		row = $stringToJSON(row);
		NCMMPubUtil.deleteVO(Trans4QMakeVO, row);
		$toast("删除成功。");
		loadScanListData();
	}
	function doCancel() {
		$toast("删除操作已取消。");
	}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showItemButton(sender, args){
    $view.openPop(popDialogInfo);
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$onresume(sender, args){
    loadScanListData();
}
function com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doCommit(sender, args){
    $js.showLoadingBar();
    $ctx.dataCollect();
    var sourceData = $ctx.getJSONArray("listdata");
    console.log($jsonToString(sourceData));
    var billinfos = [];
    var needCommit = false;
    var map = {};
    var group = [];
    //记录提交的vo
    var submitvos = [];
    //过滤需要提交的单据，并根据仓库和组织进行分组
    for(var i = 0; i < sourceData.length; i++) {
        if(sourceData[i]["isChecked"] != null && (sourceData[i]["isChecked"] == true || sourceData[i]["isChecked"] == "true")) {
            needCommit = true;
            submitvos.push(sourceData[i]);
            if (!map[sourceData[i]["pk_org"]+sourceData[i]["cwarehouseid"]]) {
                group.push({
                    "id": sourceData[i]["pk_org"]+sourceData[i]["cwarehouseid"],
                    "datas": [sourceData[i]]
                });
                map[sourceData[i]["pk_org"]+sourceData[i]["cwarehouseid"]] = sourceData[i];
            } else {
                for(var j = 0; j < group.length; j++){
                    if (group[j]["id"] == (sourceData[i]["pk_org"]+sourceData[i]["cwarehouseid"])) {
                        group[j]["datas"].push(sourceData[i]);
                        break;
                    }
                }
            }
        }
    }
    console.log($jsonToString(group));
    for (var a = 0; a < group.length; a++) {
        var billinfo = {};
        var datas = group[a]["datas"];
        //处理表头
        var head = {};
        head["pk_org"] = datas[0]["pk_org"];
        head["cwarehouseid"] = datas[0]["cwarehouseid"];
        billinfo["head"] = head;
        //处理表体,根据物料,批次号,序列号，出入货位进行合并
        var items = [];
        var itemKeys = [];
        var itemJson = {};
        var crowno = 10;
        for (var b = 0; b < datas.length; b++) {
            var itemKey = datas[b]["pk_material"]+datas[b]["vbatchcode"]+datas[b]["castunitid"]+datas[b]["vchangerate"]
                +datas[b]["zcpk_rack"]+datas[b]["zrpk_rack"];
            if (NCMMPubUtil.isEmpty(itemJson) || NCMMPubUtil.isEmpty(itemJson[itemKey])) {
                var item = {};
                item["crowno"] = crowno;
                item["pk_material"] = datas[b]["pk_material"];
                item["nassistnum"] = datas[b]["nassistnum"];
                item["nnum"] = datas[b]["nnum"];
                item["castunitid"] = datas[b]["castunitid"];
                item["cunitid"] = datas[b]["cunitid"];
                item["vchangerate"] = datas[b]["vchangerate"];
                item["zcpk_rack"] = datas[b]["zcpk_rack"];
                item["zrpk_rack"] = datas[b]["zrpk_rack"];
                item["vbatchcode"]=datas[b]["vbatchcode"];
                //item["pk_batchcode"]=datas[b]["pk_batchcode"];
                if(NCMMPubUtil.isNotEmpty(datas[b].vserialcode)){
                   var bbitem={
                       "vserialcode":datas[b].vserialcode,
                       "nnum":datas[b].nnum,
                       "nassistnum":datas[b].nassistnum
                    };
                    var bbitems = [{
				       "bbitem": bbitem
			         }];
	                item["bbitems"] = bbitems;
                }
                itemJson[itemKey] = item;
                crowno = crowno + 10;
                itemKeys.push(itemKey);
            } else {
                var newItem = itemJson[itemKey];
                newItem.nnum = getAddScanedNum(newItem.nnum, datas[b]["nnum"]);
                newItem.nassistnum = getAddScanedNum(newItem.nassistnum, datas[b]["nassistnum"]);
                if(NCMMPubUtil.isNotEmpty(datas[b].vserialcode)){
                   var bbitem={
                       "vserialcode":datas[b].vserialcode,
                       "nnum":datas[b].nnum,
                       "nassistnum":datas[b].nassistnum
                    };
                    var bbitems = {
				       "bbitem": bbitem
			         };
	                 newItem.bbitems.push(bbitems);
                }
            }
        }
        for (var c = 0; c < itemKeys.length; c++) {
            var itemjson = {
                "item": itemJson[itemKeys[c]]
            };
            items.push(itemjson);
        }
        billinfo["items"] = items;
        var billjson = {
            "billinfo": billinfo
        };
        billinfos.push(billjson);
    }
    console.log($jsonToString(billinfos));
    //测试连接
    NCMMDBUtil.testNCConnection(function(data) {
        if(data == false || data == "false") {
            $js.hideLoadingBar();
            $toast("连接服务器失败，无法提交！");
            return;
        }
        NCMMDBUtil.submitLocaldataToNC("4Qsave", billinfos, function(commitInfo) {
            if(!NCMMPubUtil.isEmpty(commitInfo)) {
                if(commitInfo.return == null || commitInfo.return == "") {
                    $js.hideLoadingBar();
                    $alert("提交失败：" + $jsonToString(commitInfo));
                } else if(commitInfo["return"].returnFlag == 1) {
                    TransMake4QUtil.deleteaftersubmit(submitvos);
                    loadScanListData();
                    $js.hideLoadingBar();
                    $toast("提交成功！");
                } else {
                    $js.hideLoadingBar();
                    $alert("提交失败" + $jsonToString(commitInfo.return.returnDesc));
                }
            } else {
                $js.hideLoadingBar();
                $alert("提交失败");
            }
        });
    });
}

    function getAddScanedNum(scanedNum, currentScanedNum) {
		if(scanedNum == null || scanedNum == "") {
			scanedNum += "00.00000";
		} else {
			scanedNum = parseFloat(scanedNum);
		}
		if(currentScanedNum == null || currentScanedNum == "") {
			currentScanedNum += "1.00000";
		} else {
			currentScanedNum = parseFloat(currentScanedNum);
		}
		var newScanedNum = parseFloat(scanedNum).add(parseFloat(currentScanedNum));
		return newScanedNum;
	}

com.yonyou.ncmm_pac_pda.Trans4QMakeListController.prototype = {
    doCommit : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doCommit,
    onresume : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$onresume,
    doScan: com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doScan,
	showDetail: com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showDetail,
    itemDelete: com$yonyou$ncmm_pac_pda$Trans4QMakeListController$itemDelete,
	itemCancel: com$yonyou$ncmm_pac_pda$Trans4QMakeListController$itemCancel,
    showItemButton : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showItemButton,
    loadScanList : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$loadScanList,
    doAllChecked : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doAllChecked,
    doChecked : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$doChecked,
    showMenu : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$showMenu,
    close : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$close,
    initialize : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$initialize,
    evaljs : com$yonyou$ncmm_pac_pda$Trans4QMakeListController$evaljs
};
com.yonyou.ncmm_pac_pda.Trans4QMakeListController.registerClass('com.yonyou.ncmm_pac_pda.Trans4QMakeListController',UMP.UI.Mvc.Controller);
}catch(e){$e(e);}
